package chances.wechat.service;

import java.io.StringWriter;
import java.net.URLEncoder;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;

import chances.wechat.entity.UserRequest;

@Service
public class UrlFactory {

	@Autowired
	private VelocityConfigurer velocityConfigurer;

	public String createUrl(UserRequest cmd, String reqType) {
		VelocityEngine velocityEngine = velocityConfigurer.getVelocityEngine();
		Template template = velocityEngine.getTemplate(cmd.getType() + ".vm");
		VelocityContext context = new VelocityContext();
		context.put("content", cmd);
		context.put("encoder", URLEncoder.class);
		context.put("reqType", reqType);
		StringWriter writer = new StringWriter();
		template.merge(context, writer);
		return writer.toString();
	}

}
